package ua.yandex.shad.tempseries;

import static java.lang.Math.sqrt;

public class TemperatureSeriesAnalysis {
    
    private final double[] temperatureSeries;
    static final double LowestTemp = -273;

    public class IllegalTemperatureException extends RuntimeException {
        public IllegalTemperatureException() 
        { 
        }
        public IllegalTemperatureException(String message) 
        { 
        }
        public IllegalTemperatureException(String message, Throwable cause) 
        { 
        }
        public IllegalTemperatureException(Throwable cause) 
        {
        }
    }
    public TemperatureSeriesAnalysis(double[] temperatureSeries) throws IllegalTemperatureException {
        this.temperatureSeries = temperatureSeries;
        for (int i=1; i<=temperatureSeries.length; i++)
        {
            if (temperatureSeries[i-1]<=LowestTemp) { throw new IllegalTemperatureException("Temperature below -273C detected!"); }
        }
    }
    
    public double calcAverageTemp(){
        double avg = 0;
        int i;
        for (i=1; i<= temperatureSeries.length ; i++)
        {
            avg = avg + temperatureSeries[i-1];
        }
        if (temperatureSeries.length != 0){
            avg = avg / temperatureSeries.length;
        }
        return avg;
    }    
    
    public double calcTempDeviation(){
        double dev = 0;
        double avg;
        avg = calcAverageTemp();
        for (int i = 1; i<=temperatureSeries.length; i++){
            dev = dev + (temperatureSeries[i-1]-avg)*(temperatureSeries[i-1]-avg);
        }
        if (temperatureSeries.length != 0){
            dev = dev / temperatureSeries.length;
            dev = sqrt(dev);
        }
        return dev;
    }
    
    public double findTempClosestToZero(){
        return findTempClosestToValue(0);
    }
    
    public double findTempClosestToValue(double tempValue){
        double closestToValue = 0;
        if (temperatureSeries.length != 0) {
            closestToValue = temperatureSeries[0];
            int i;
            for (i=2; i<= temperatureSeries.length; i++){
                if (Math.abs(closestToValue-tempValue) > Math.abs(temperatureSeries[i-1]-tempValue)){
                    if (temperatureSeries[i-1] > closestToValue) {
                        closestToValue = temperatureSeries[i-1];
                    }
                }
            }
        }
        return closestToValue;
    }
    
    public double[] findTempsLessThen(double tempValue)
    {
        double [] tempsLessThen;
        int k=0;
        int i;
        for (i=0; i < temperatureSeries.length ; i++)
        {
            if (temperatureSeries[i] < tempValue) { k++; }
        }
        int j = 0;
        tempsLessThen = new double [k];
        for (i=0; i < temperatureSeries.length; i++)
        {
            if (temperatureSeries[i] < tempValue) 
            {
                tempsLessThen[j] = temperatureSeries[i];
                j++;
            }
        }
        return tempsLessThen;
    }
    
    public double[] findTempsGreaterThen(double tempValue)
    {
        double [] tempsGreaterThen;
        int k=0;
        int i;
        for (i=0; i < temperatureSeries.length ; i++)
        {
            if (temperatureSeries[i] > tempValue) { k++; }
        }
        int j = 0;
        tempsGreaterThen = new double [k];
        for (i=0; i < temperatureSeries.length; i++)
        {
            if (temperatureSeries[i] > tempValue) 
            {
                tempsGreaterThen[j] = temperatureSeries[i];
                j++;
            }
        }
        return tempsGreaterThen;
    }
}
